﻿using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TfsWrapper.Commands
{
    public abstract class TfsCommand
    {
        static readonly IDictionary<string, Func<IOutputWriter, TfsCommand>> Commands = 
            new Dictionary<string, Func<IOutputWriter, TfsCommand>>
            {
                { "FindMentionsByChangeset", writer => new FindMentionsByChangesetCommand(new IssueMentionPrinter(writer)) },
                { "GetById", writer => new GetByIdCommand(new IssueDataPrinter(writer)) },
            };

        public static IEnumerable<string> CommandNames
        {
            get { return Commands.Keys; }
        }

        public static TfsCommand FromName(string name, IOutputWriter output)
        {
            Func<IOutputWriter, TfsCommand> command;

            if (Commands.TryGetValue(name, out command))
                return command(output);

            throw new NotSupportedException(string.Format("Command with name {0} not supported", name));
        }

        public abstract void Execute(TfsConnection tfs, params string[] args);

        protected static string MakeWorkItemUrl(TfsConnection tfs, WorkItem workItem)
        {
			return tfs.Uri.ToString().TrimEnd('/') + string.Format("/{0}/_workitems/edit/{1}", workItem.Project.Name, workItem.Id);
        }
    }
}